package com.qingfeng.client.api;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qingfeng.client.DO.GoodsDetailAllInfo;
import com.qingfeng.client.DO.GoodsDetailDo;
import com.qingfeng.client.DO.GoodsSimpInfo;
import com.qingfeng.client.VO.GoodsConditionVo;
import com.qingfeng.client.VO.GoodsQueryVo;
import com.qingfeng.client.bean.GoodsCommon;
import com.qingfeng.client.service.GoodsService;
import com.qingfeng.client.util.Page;
import com.qingfeng.util.ApiMsg;
import com.qingfeng.util.OkhttpUtil;
import com.qingfeng.util.SignUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 商品接口
 * @author zhangyuefei
 * @version 1.0
 * @date 2020/3/6 4:51 下午
 */
@Api(tags = "商品相关接口")
@RestController
@RequestMapping("/app/goods")
public class GoodsApi extends BaseV1Api{

    @Autowired
    private GoodsService goodsService;

    @ApiOperation(value="搜索商品")
    @PostMapping("/list")
    public ApiMsg<IPage<GoodsSimpInfo>> find(@RequestBody GoodsQueryVo goodsQueryVo){
        Page page = goodsQueryVo.getPage();
        GoodsConditionVo condition = goodsQueryVo.getCondition();
        IPage page1 = goodsService.getPage(page,condition);
        return successPage(page1);
    }


    @ApiOperation("获取商品详情")
    @GetMapping("/detail/{id}")
    public ApiMsg<GoodsDetailAllInfo> detail( @ApiParam(name = "id",value = "id为goodsCommonId",required = true)
                                                  @PathVariable("id")Long goodsCommonId){
        GoodsDetailAllInfo goodsDetailAllInfo = goodsService.detail(goodsCommonId);
        return success(goodsDetailAllInfo);
    }

    @RequestMapping(value = "/share2")
    @ResponseBody
    public Object shareGoodsDetail(HttpServletRequest request, @RequestBody String url) throws Exception {
        System.out.println("微信分享");
        //第一步，获取access_token
        String access_token = SignUtil.getToken();
        //第二步，获取jsapi_ticket
        String strUrl = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + access_token + "&type=jsapi";
        String result = OkhttpUtil.get(strUrl);
        System.out.println(result);
        String jsapi_ticket = JSONObject.parseObject(result).getString("ticket");
        if (StringUtils.isEmpty(jsapi_ticket)){
            SignUtil.setTokenEmpty();
        }
        // 注意 URL 一定要动态获取，不能 hardcode
        //当前网页的url
        //服务器地址 //端口号 //项目名称 //请求页面或其他地址 //参数
        System.out.println(url);
        Map<String, String> res = SignUtil.sign(jsapi_ticket, url);
        System.out.println(res);
        return success(res);
    }

}
